# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: index.py
Author: kij
Create Date: 2021/2/19
-------------------------------------------------
"""
import sys
from user.user_data import UserUtils, StudentRole, AdminRole,TeacherRole
from .select_func import select_func, get_user
from conf.config import ROLE_TUPLE


def register(role_name):
	print("欢迎来到注册功能")
	if hasattr(sys.modules[__name__], role_name):
		role = getattr(
			sys.modules[__name__], role_name)
		UserUtils.register(role)
	loading_logon(role_name)


# 登陆接口
def logon(role_name):
	print("欢迎登录选课系统")
	is_user = UserUtils.logon()
	if is_user != None and is_user.role_name == role_name:
		print("登陆成功")
		logon_dic.get(is_user.role_name)[0](is_user)
	# logon_dic[is_user.role_name](is_user)
	else:
		print("登陆失败")
		loading_logon(role_name)


def student_view(user=None):
	print("学员视图".center(50, "*"))
	if user == None:
		loading_logon(ROLE_TUPLE[1])
	else:
		select_func(user.my_view, student_view, user)


def teacher_view(user=None):
	print("讲师视图".center(50, "*"))
	if user == None:
		logon(ROLE_TUPLE[2])
	else:
		select_func(user.my_view, teacher_view, user)


def admin_view(user=None):
	print("管理视图".center(50, "*"))
	if user == None:
		loading_logon(ROLE_TUPLE[0])
	else:
		select_func(user.my_view, admin_view, user)


def loading_logon(role_name):
	for num, item in enumerate(secound_list, 0):
		print("%s.%s" % (num, item[0]))
	show_view = input(">>>")
	select_num = ['0', '1', '2']
	if show_view not in select_num:
		loading_logon(role_name)
	if show_view == "0":
		run()
	# 登录和注册
	secound_list[int(show_view)][1](role_name)


first_list = [
	('退出程序', None),
	('管理视图', admin_view),
	('学员视图', student_view),
	('讲师视图', teacher_view),
]

logon_dic = {
	'AdminRole': [admin_view],
	'StudentRole': [student_view],
	'TeacherRole': [teacher_view],
}
secound_list = [
	('返回上一级', None),
	('登录', logon),
	('注册', register),
]


def run():
	for num, item in enumerate(first_list, 0):
		print("%s.%s" % (num, item[0]))
	print("""输入进入的视图序号""")
	show_view = input(">>>")
	select_num = ['0', '1', '2', '3']
	if show_view not in select_num:
		run()
	if show_view == "0":
		exit()
	first_list[int(show_view)][1]()
